package icepuppet.test;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame1 extends JFrame implements ActionListener {
    JButton jButton1 = new JButton("点我呀");
    JButton jButton2 = new JButton("点我呀");

    public MyJFrame1(){
        //设置长宽
        this.setSize(400, 400);
        //设置标题
        this.setTitle("事件练习");
        //设置界面始终位于最上方
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中
        this.setLayout(null);

        //创建一个按钮
        //设置位置和宽高
        jButton1.setBounds(0,0,100,50);
        //创建一个按钮
        //设置位置和宽高
        jButton2.setBounds(100,0,100,50);
        //设置动作监听
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);

        //将按钮添加到界面里
        this.getContentPane().add(jButton1);
        this.getContentPane().add(jButton2);

        //显示
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取对象
        Object source = e.getSource();
        if(source == jButton1){
            jButton1.setLocation(100,50);
        }else if(source == jButton2){
            jButton2.setLocation(200,100);
        }
    }
}
